package cn.edu.sgu.www.mhxysy.service.equipment.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.equipment.EquipmentTexiaoDTO;
import cn.edu.sgu.www.mhxysy.entity.equipment.EquipmentTexiao;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentTexiaoMapper;
import cn.edu.sgu.www.mhxysy.service.equipment.EquipmentTexiaoService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class EquipmentTexiaoServiceImpl implements EquipmentTexiaoService {

	private final EquipmentTexiaoMapper equipmentTexiaoMapper;

	@Autowired
	public EquipmentTexiaoServiceImpl(EquipmentTexiaoMapper equipmentTexiaoMapper) {
		this.equipmentTexiaoMapper = equipmentTexiaoMapper;
	}

	@Override
	public void insert(EquipmentTexiaoDTO equipmentTexiaoDTO) {
		EquipmentTexiao equipmentTexiao = toEntity(equipmentTexiaoDTO);

		equipmentTexiao.setId(null);

		equipmentTexiaoMapper.insert(equipmentTexiao);
	}

	@Override
	public void updateById(EquipmentTexiaoDTO equipmentTexiaoDTO) {
		ObjectUtils.requireNonNull(equipmentTexiaoDTO.getId(), CustomMessages.ID_NOT_NULL);

		EquipmentTexiao equipmentTexiao = toEntity(equipmentTexiaoDTO);

		equipmentTexiaoMapper.updateById(equipmentTexiao);
	}

	@Override
	public List<EquipmentTexiao> selectAll() {
		QueryWrapper<EquipmentTexiao> queryWrapper = new QueryWrapper<>();

		queryWrapper.orderByAsc("type");

		return equipmentTexiaoMapper.selectList(queryWrapper);
	}

	@Override
	public EquipmentTexiao selectById(Integer id) {
		return equipmentTexiaoMapper.selectById(id);
	}

	@Override
	public Page<EquipmentTexiao> selectByPage(Pager<EquipmentTexiao> pager) {
		QueryWrapper<EquipmentTexiao> queryWrapper = Pager.getQueryWrapper(pager);
		Page<EquipmentTexiao> page = Pager.ofPage(pager);

		return equipmentTexiaoMapper.selectPage(page, queryWrapper);
	}

	/**
	 * DTO转数据库实体类
	 * @param equipmentTexiaoDTO EquipmentTexiaoDTO对象
	 * @return 根据EquipmentTexiaoDTO对象封装得到的EquipmentTexiao对象
	 */
	private EquipmentTexiao toEntity(EquipmentTexiaoDTO equipmentTexiaoDTO) {
		EquipmentTexiao equipmentTexiao = new EquipmentTexiao();

		equipmentTexiao.setId(equipmentTexiaoDTO.getId());
		equipmentTexiao.setName(equipmentTexiaoDTO.getName());
		equipmentTexiao.setType(equipmentTexiaoDTO.getType());
		equipmentTexiao.setNote(equipmentTexiaoDTO.getNote());
		equipmentTexiao.setScore(equipmentTexiaoDTO.getScore());
		equipmentTexiao.setPropertyId(equipmentTexiaoDTO.getPropertyId());
		equipmentTexiao.setPropertyValue(equipmentTexiaoDTO.getPropertyValue());

		return equipmentTexiao;
	}

}